package org.streets.workflow.engine.mem;

import java.util.ArrayList;
import java.util.List;

import org.streets.workflow.engine.ITransition;
import org.streets.workflow.engine.event.IEdgeEventListener;
import org.streets.workflow.engine.plugin.IPlugable;
import org.streets.workflow.engine.plugin.INodeNetExtension;
import org.streets.workflow.model.net.WFTransition;


/**
 *
 */
public class Transition extends AbstractEdge implements ITransition, IPlugable {

    public transient static final String ExtensionTargetName = "org.streets.workflow.mem.Transition";
    public transient static final String ExtensionPointTransitionEventListener = "TransitionEventListener";
    public transient static List<String> ExtensionPointNames = new ArrayList<String>();
    
    static {
        ExtensionPointNames.add(ExtensionPointTransitionEventListener);
    }

    private transient WFTransition transition = null;

    public Transition(WFTransition t) {
        transition = t;
    }
    
    public String getEdgeId() {
        return this.transition.getId();
    }
    
    
    @SuppressWarnings("unchecked")
    public WFTransition getEdgeModel() {
        return this.transition;
    }

    //获取转移线上的权值
    /* (non-Javadoc)
     * @see org.fireflow.kernel.IEdgeInstance#getWeight()
     */
    public int getWeight() {
        if (weight == 0) {
            if (enteringNode instanceof StartNode) {
                weight=1;
                return weight;
                //如果前驱结点是开始节点，那么权值规定为1
            } else if (leavingNode instanceof EndNode) {
                weight=1;
                return weight;
                //如果后继结点为结束节点，那么权值规定为1
            } else if (leavingNode instanceof Activity) {
                SyncRouter synchronizerInstance = (SyncRouter) enteringNode;
                int weight = synchronizerInstance.getVolume() / enteringNode.getLeavingTransitions().size();
                return weight;
                //如果弧线的后继结点 是 task结点，那么弧线的权值=前驱同步器结点的容量/输出弧线的数量
            } else if (leavingNode instanceof SyncRouter) {
                SyncRouter synchronizerInstance = (SyncRouter) leavingNode;
                int weight = synchronizerInstance.getVolume() / leavingNode.getEnteringTransitions().size();
                return weight;
                //如果后继结点是同步器节点，那么权值=同步器的容量/同步器的输入弧线的数量
            }
        }

        return weight;
    }
//
//    /* (non-Javadoc)
//     * @see org.fireflow.kernel.IEdgeInstance#take(org.fireflow.kernel.IToken)
//     */
//    public boolean take(IToken token) throws WorkflowException {
//        EdgeEvent e = new EdgeEvent(this);
//        e.setToken(token);
//        e.setEventType(EdgeEvent.ON_TAKING_THE_TOKEN);
//
//        for (int i = 0; this.eventListeners != null && i < this.eventListeners.size(); i++) {
//            IEdgeEventListener listener =  this.eventListeners.get(i);
//            listener.onEdgeEventFired(e); //调用TransitionInstanceExtension 来计算弧线上的条件表达式
//        }
//
//        INode nodeInst = this.getLeavingNode(); //获取到流向哪个节点
//        token.setValue(this.getWeight());//获取到弧线上的权值
//        boolean alive = token.isAlive();
//
//        nodeInst.fire(token);//节点触发
//
//        return alive;
//    }


    @SuppressWarnings("static-access")
	public String getExtensionTargetName() {
        return this.ExtensionTargetName;
    }

    @SuppressWarnings("static-access")
	public List<String> getExtensionPointNames() {
        return this.ExtensionPointNames;
    }

    public void register(INodeNetExtension extension) throws RuntimeException {
    	if (!ExtensionTargetName.equals(extension.getExtentionTargetName())) {
            return;
    	}
        if (ExtensionPointTransitionEventListener.equals(extension.getExtentionPointName())) {
            if (extension instanceof IEdgeEventListener) {
                this.eventListeners.add((IEdgeEventListener) extension);
            } else {
                throw new RuntimeException("Error:When construct the TransitionInstance,the extension MUST be a instance of ITransitionInstanceEventListener");
            }
        }
    }
}
